using System.Collections.Generic;
using System.Linq;
using System;

namespace DesktopApp.ViewModels
{
    public class RuleEditorViewModel : ViewModelBase
    {
        private string _name = string.Empty;
        public string Name
        {
            get => _name;
            set => SetProperty(ref _name, value);
        }

        private string _description = string.Empty;
        public string Description
        {
            get => _description;
            set => SetProperty(ref _description, value);
        }

        private RuleType _selectedRuleType;
        public RuleType SelectedRuleType
        {
            get => _selectedRuleType;
            set
            {
                if (SetProperty(ref _selectedRuleType, value))
                {
                    // Update visibility/requirements based on type if needed in UI
                    OnPropertyChanged(nameof(IsKeywordVisible));
                    OnPropertyChanged(nameof(IsReplacementVisible));
                    OnPropertyChanged(nameof(IsPatternVisible));
                }
            }
        }

        // Provide RuleType enum values for ComboBox binding
        public IEnumerable<RuleType> RuleTypes => Enum.GetValues(typeof(RuleType)).Cast<RuleType>();

        private string? _keyword;
        public string? Keyword
        {
            get => _keyword;
            set => SetProperty(ref _keyword, value);
        }

        private string? _replacement;
        public string? Replacement
        {
            get => _replacement;
            set => SetProperty(ref _replacement, value);
        }

        private string? _pattern;
        public string? Pattern
        {
            get => _pattern;
            set => SetProperty(ref _pattern, value);
        }

        // --- UI Helper Properties (for conditional visibility in XAML) ---
        public bool IsKeywordVisible => SelectedRuleType == RuleType.FilterExcludes || SelectedRuleType == RuleType.FilterIncludes || SelectedRuleType == RuleType.KeywordReplace;
        public bool IsReplacementVisible => SelectedRuleType == RuleType.KeywordReplace || SelectedRuleType == RuleType.RegexReplace;
        public bool IsPatternVisible => SelectedRuleType == RuleType.RegexReplace;

        // New property to encapsulate the logic for the 'no parameters' message
        public bool ShowNoParametersMessage => !(IsKeywordVisible || IsReplacementVisible || IsPatternVisible);

        public RuleEditorViewModel(RuleItemViewModel? existingRule = null)
        {
            if (existingRule != null)
            {
                // Populate from existing rule for editing
                Name = existingRule.Name;
                Description = existingRule.Description;
                SelectedRuleType = existingRule.Type;
                Keyword = existingRule.Keyword;
                Replacement = existingRule.Replacement;
                Pattern = existingRule.Pattern;
            }
            else
            {
                // Default for new rule
                SelectedRuleType = RuleType.FilterExcludes;
            }
        }

        public RuleItemViewModel? GetRuleItemViewModel()
        {
            // Basic validation
            if (string.IsNullOrWhiteSpace(Name))
            {
                // Handle validation error (e.g., show message)
                System.Diagnostics.Debug.WriteLine("Validation Error: Rule Name is required.");
                return null;
            }

            return new RuleItemViewModel
            {
                Name = this.Name,
                Description = this.Description,
                Type = this.SelectedRuleType,
                Keyword = this.Keyword,
                Replacement = this.Replacement,
                Pattern = this.Pattern,
                IsEnabled = true // Default new/edited rules to enabled?
            };
        }
    }
} 